package com.lx.huimailf.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lx.huimailf.R;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.HashMap;

public class PubAdapter extends RecyclerView.Adapter<PubAdapter.MyViewHolder>{
    private Context mContext;
    private View mPingjiaView;
    ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();
    private DisplayMetrics dm;
    public interface OnItemClickLitener
    {
        void onItemClick(View view, int position);
    }

    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener)
    {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }
    public PubAdapter(Context mContext, ArrayList<HashMap<String, Object>> listItem){
        this.mContext=mContext;
        this.listItem=listItem;

        dm = new DisplayMetrics();
        ((Activity) mContext).getWindowManager().getDefaultDisplay()
                .getMetrics(dm);
    }
    @Override
    public PubAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        PubAdapter.MyViewHolder holder = new PubAdapter.MyViewHolder(LayoutInflater.from(
                mContext).inflate(R.layout.item_pub, parent,
                false));
        return holder;
    }

    @Override
    public void onBindViewHolder(final PubAdapter.MyViewHolder holder, final int position) {

        holder.price.setText(listItem.get(position).get("price").toString()+"");
        holder.count.setText(listItem.get(position).get("count").toString()+"");
        holder.proname.setText(listItem.get(position).get("proname").toString()+"");
        Picasso.with(mContext).load(listItem.get(position).get("picname").toString()).into(holder.logo);

        // 如果设置了回调，则设置点击事件
        if (mOnItemClickLitener != null)
        {
            holder.itemView.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemClick(holder.itemView, pos);
                }
            });
        }
    }

    @Override
    public int getItemCount()
    {
        return listItem.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder
    {

        public TextView price,count,proname;
        public ImageView logo;
        public MyViewHolder(View view)
        {
            super(view);
            price = (TextView) view.findViewById(R.id.textView3);
            count = (TextView) view.findViewById(R.id.textView2);
            proname = (TextView) view.findViewById(R.id.textView1);
            logo=(ImageView)view.findViewById(R.id.logo);
        }
    }
}

